package com.lt.flashcard.model.entity;

// Generated Nov 23, 2012 2:01:25 PM by Hibernate Tools 4.0.0

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import com.lt.flashcard.model.service.Repository;

/**
 * Productcategory generated by hbm2java
 */
@Entity
@Table(name = "productcategory", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Productcategory implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Long id;
	private Productcategory parent;
	private String slug = "";
	private String name;
	private int weight;
	/**
	 * Display on the GUI or not
	 */
	private int display = 1;
	private Set<FlashcardSet> flashcardsets = new HashSet<FlashcardSet>(0);
	private List<Productcategory> child = new ArrayList<Productcategory>(0);

	public Productcategory() {
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ProductCategory_id", nullable = true)
	public Productcategory getParent() {
		return this.parent;
	}

	public void setParent(Productcategory parent) {
		this.parent = parent;
	}

	@Column(name = "name", unique = true, nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "weight", nullable = false)
	public int getWeight() {
		return this.weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "productcategory")
	public Set<FlashcardSet> getFlashcardsets() {
		return this.flashcardsets;
	}

	public void setFlashcardsets(Set<FlashcardSet> flashcardsets) {
		this.flashcardsets = flashcardsets;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", cascade={CascadeType.ALL})
	public List<Productcategory> getChild() {
		return this.child;
	}

	public void setChild(List<Productcategory> child) {
		this.child = child;
	}

	/**
	 * Remove a child
	 * @param id
	 */
	@Transient
	public Productcategory removeChild(Long id) {
		for(Productcategory c : child) {
			if(c.getId() != null && c.getId().equals(id)) {
				child.remove(c);
				return c;
			}
		}
		return null;
	}
	
	/**
	 * Get list of products
	 * @return
	 */
	private List<FlashcardSet> _products = null;
	@Transient
	public List<FlashcardSet> getProducts() {
		if(_products == null)
			_products = Repository.getInstance().getEcommerceService().getProductsOfCategory(this.getId());
		return _products;
	}
	
	/**
	 * Remove a child
	 * @param cat
	 */
	public void removeChild(Productcategory cat) {
		child.remove(cat);
	}

	/**
	 * @return the display
	 */
	@Column(name = "display", nullable = false)
	public int getDisplay() {
		return display;
	}

	/**
	 * @param display the display to set
	 */
	public void setDisplay(int display) {
		this.display = display;
	}

	/**
	 * @return the slug
	 */
	@Column(name = "slug", nullable = false)
	public String getSlug() {
		return slug;
	}

	/**
	 * @param slug the slug to set
	 */
	public void setSlug(String slug) {
		this.slug = slug;
	}
}
